package com.afiorenza.one;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.afiorenza.one.factory.VehiculoInterface;

public class VehiculoHelper {
	private static File file = new File("resources/Vehiculos.txt");

	public static List<VehiculoInterface> leerArchivo() {

		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			VehiculoFactory v = new VehiculoFactory();
			String s;
			List<VehiculoInterface> vehiculos = new ArrayList<VehiculoInterface>();

			while ((s = in.readLine()) != null) {

				String[] var = s.split(";");
				List<Object> l = new ArrayList<Object>(Arrays.asList(var));
				vehiculos.add(v.getVehiculo(l));
			}

			return vehiculos;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static void ordenarPorFecha(List<VehiculoInterface> vehiculoList) {
		int i;
		int j;
		VehiculoInterface temp;
		for (i = 0; i < vehiculoList.size() - 1; i++) {
			for (j = 0; j < vehiculoList.size() - i - 1; j++) {
				if (vehiculoList.get(j + 1).getFechaPatentamiento().before(vehiculoList.get(j).getFechaPatentamiento())) {
					temp = vehiculoList.get(j + 1);
					vehiculoList.set(j + 1, vehiculoList.get(j));
					vehiculoList.set(j, temp);
				}
			}
		}

	}

	public static void listar(List<VehiculoInterface> v) {
		for (VehiculoInterface vehiculoInterface : v) {
			vehiculoInterface.print();
		}
	}
}
